package com.kfm.springmybatis.service;

import com.github.pagehelper.PageHelper;
import com.kfm.springmybatis.dao.StudentDao;
import com.kfm.springmybatis.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StudentService {
//    StudentDao dao;
//    SqlSession sqlSession;
//    InputStream inputStream;
//    public StudentService(){
//        try {
//            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//        SqlSessionFactory build = builder.build(inputStream);
//        sqlSession = build.openSession(true);
//        dao = sqlSession.getMapper(StudentDao.class);
//    }

    @Autowired
    StudentDao dao;
    /*
    查询学生信息
     */

    public List<Student> queryStudent(){
        return dao.selectAll();
    }

    /*
    * 添加学生信息
     */
    public int addStudent(Student student){
        return dao.insertStudent(student);
    }
    /*
    * 修改学生信息
     */
    public int updateStudent(Student student){
        return dao.updateStudent(student);
    }
    /*
    * 删除学生信息
     */
    public int deleteStudent(int id){
        return dao.deleteStudent(id);
    }

    /*
    分页查询学生信息
     */
    public List<Student> queryStudentByPage(int page,int pageSize){
        PageHelper.startPage(1, 5);
        return dao.queryStudentsWithPaging(page, pageSize);
    }


}
